using System;
using System.Windows.Forms;
using QSBusinessLayerCSharp;
using ValidationFramework;

namespace QSWindowsApplicationCSharp
{
    public partial class RuleSetForm : Form
    {
        private readonly Vehicle vehicle;
        public RuleSetForm()
        {
            InitializeComponent();
            vehicle = new Vehicle();
        }

        private void RuleSetForm_Load(object sender, EventArgs e)
        {
            vehicleBindingSource.Add(vehicle);
            ruleSetListBox.SelectedIndex = 0;
        }

        private void validateButton_Click(object sender, EventArgs e)
        {
            var selectedText = (string)ruleSetListBox.SelectedItem;
            var manager = new PropertyValidationManager(vehicle)
                          	{
                          		RuleSet = selectedText
                          	};
            manager.ValidateAll();
            errorTreeView.Nodes.Clear();
            foreach (var result in manager.ValidatorResultsInError)
            {

                var node = new TreeNode(result.ErrorMessage);
                node.Nodes.Add(String.Format("RuleInterpretation = '{0}'", result.Rule.RuleInterpretation));
                node.Nodes.Add(String.Format("Rule = '{0}'", result.Rule.GetType()));
                errorTreeView.Nodes.Add(node);
            }
        }
    }
}